 /*
 * @description: app版本管理
 * @Author: along
 * @Date: 2019-09-10
 * @Last Modified by: along
 * @Last Modified time: 2019-09-10
 */
<template>
    <div class="container appConfig">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <el-tag
                        type="danger"
                    >Ios最新版本:{{ version.version_ios ? version.version_ios : '--' }}</el-tag>
                    <el-tag
                        type="danger"
                        :style="{marginRight: '30px', marginLeft: '30px'}"
                    >Android最新版本:{{ version.version_android ? version.version_android : '--' }}</el-tag>
                    <el-tag
                        type="danger"
                    >更新机制:5-10分钟</el-tag>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="showVersion()"
                    >
                        设置版本号
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="appConfig"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="update_time"
                label="最近更新时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.update_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="app_comment"
                label="app评论数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                prop="app_release_time"
                label="app发布时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.app_release_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="app_download"
                label="app下载数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                prop="app_release_note"
                label="app发布日志"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="180"
            />
            <el-table-column
                prop="app_name"
                label="app名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="market_name"
                label="市场名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="app_url"
                label="app链接"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="190"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="fnCopy(scope.row.app_url)"
                    >{{ scope.row.app_url }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="app_version_code"
                label="app内部版本号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="140"
            />
            <el-table-column
                prop="app_score"
                label="app得分"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="market_title"
                label="应用市场"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="160"
            />
            <el-table-column
                prop="app_icon"
                label="app图标"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.app_icon"
                        :style="{width: '40px', height: '40px'}"
                        @click="fnShowPreview(scope.row.app_icon)"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="app_version"
                label="app版本号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                prop="has_comment"
                label="是否有评论"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            >
                <template slot-scope="scope">
                    {{ scope.row.has_comment ? '是' : '否' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="is_latest"
                label="是否最新"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.is_latest ? '是' : '否' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="app_category"
                label="app分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            />
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="getComment(1, scope.row)"
                    >
                        查看评论
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <el-dialog
            :visible.sync="dialogVisib"
            width="660px"
            title="评论列表"
            :close-on-click-modal="false"
        >
            <el-table
                :data="dataList"
                height="100%"
                style="width: 100%;max-height:500px"
                border
            >
                <el-table-column
                    prop="user_name"
                    label="用户名称"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="score"
                    label="评分"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="content"
                    label="评论内容"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="version"
                    label="版本号"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="create_time"
                    label="app发布时间"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                    :width="150"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getComment"
                />
            </div>
        </el-dialog>

        <el-dialog
            :visible.sync="dialogVisibVersion"
            width="660px"
            title="设置版本号"
            :close-on-click-modal="false"
        >
            <div class="container-flex">
                <p :style="{marginRight: '10px'}">选择机型:</p>
                <el-select
                    v-model="type"
                    clearable
                    filterable
                    placeholder="请选择"
                    size="small"
                >
                    <el-option
                        v-for="item in list"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
                <el-input
                    v-if="type == 'ios'"
                    v-model="formData.version"
                    placeholder="请输入Ios最新版本号"
                    size="small"
                    clearable
                    :style="{width: '210px', marginLeft: '30px'}"
                />
                <el-input
                    v-if="type == 'android'"
                    v-model="formData.version"
                    placeholder="请输入Android最新版本号"
                    size="small"
                    clearable
                    :style="{width: '210px', marginLeft: '30px'}"
                />
            </div>
            <div class="container-foot">
                <el-button
                    type="primary"
                    size="small"
                    :disabled="!formData.version"
                    :style="{width: '120px'}"
                    @click="setVersionNum()"
                >
                    确 定
                </el-button>
            </div>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            type: 'ios',
            dialogVisibVersion: false,
            dialogVisib: false,
            dataList: [],
            market_name: '',
            formData: {
                version: ''
            },
            version: {
                version_ios: '',
                version_android: ''
            },
            list: [{
                value: 'ios',
                label: '苹果'
            },{
                value: 'android',
                label: '安卓'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialogVisibVersion (val) {
            if(!val) {
                this.formData.version = '';
                this.type = 'ios';
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: 'app版本管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getVersion();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/market%5Cget_app_list', {}, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                }
            });
        },

        /**
         * @description 查询版本号
         */
        getVersion () {
            this.$post('/student/market%5Cget_app_config', {}, resp => {
                if (resp.code == 1) {
                    this.version = {
                        ...resp.data
                    };
                }
            });
        },

        /**
         * @description 打开设置版本号弹窗
         */
        showVersion () {
            if(!this.authResult.auth_config) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.dialogVisibVersion = !this.dialogVisibVersion;
        },

        /**
         * @description 确定设置版本号
         */
        setVersionNum () {
            if(this.type == 'ios') {
                this.$post('/student/market%5Cset_app_config', {
                    version_ios: this.formData.version,
                    version_android: this.version.version_android
                }, resp => {
                    if (resp.code == 1) {
                        this.$notify({
                            title: '提示',
                            message: '设置成功',
                            type: 'success',
                            duration: 2000
                        });
                    }
                });
            }

            if(this.type == 'android') {
                this.$post('/student/market%5Cset_app_config', {
                    version_ios: this.version.version_ios,
                    version_android: this.formData.version
                }, resp => {
                    if (resp.code == 1) {
                        this.$notify({
                            title: '提示',
                            message: '设置成功',
                            type: 'success',
                            duration: 2000
                        });
                    }
                });
            }
            this.formData.version = '';
            this.getVersion();
            this.getTableData();
            this.dialogVisibVersion = !this.dialogVisibVersion;
        },

        /**
         * @description 获取评论列表
         */
        getComment (page, row) {
            this.market_name = row ? row.market_name : this.market_name;

            this.$post('/student/market%5Cget_app_comment_list', {
                page_id: page || this.page.now_page,
                market_name: this.market_name
            }, resp => {
                if (resp.code == 1) {
                    this.dialogVisib = true;
                    if(resp.data === null) {
                        this.$notify({
                            title: '提示',
                            message: '数据为空',
                            type: 'warning',
                            duration: 2000
                        });
                        return;
                    }

                    this.dataList = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 复制链接
         */
        fnCopy (content) {
            if(content) {
                let str = content;
                let oInput = document.createElement("input");

                oInput.value = str;
                document.body.appendChild(oInput);
                oInput.select();
                document.execCommand("Copy");
                oInput.style.display = "none";
                this.$message.success('复制成功');
            } else {
                this.$notify({
                    title: '提示',
                    message: '复制失败',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .container-flex {
        display: flex;
        height: 32px;
        align-items: center;
    }
    .container-foot {
        width: 100%;
        height: 32px;
        display: flex;
        justify-content: center;
        margin-top: 30px;
    }
</style>
<style lang="less">
    .appConfig{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
    }
</style>
